import {Inject, Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Document} from './document.model';
import {tap} from 'rxjs/internal/operators';
import {AuthHttp} from '../../shared/auth.http';
import {APP_CONFIG} from '../../config/app.config';

@Injectable()
export class DocumentService {
  private baseUrl: string;
  private currentSelectedDocument: Document;

  constructor(@Inject(APP_CONFIG) appConfig,
              private http: AuthHttp) {
    this.baseUrl = appConfig.endpoints.base;
  }

  get currentDocument(): Document {
    return this.currentSelectedDocument;
  }

  set currentDocument(currentSelectedDocument: Document) {
    this.currentSelectedDocument = currentSelectedDocument;
  }

  getDocumentList(): Observable<Document[]> {
    return this.http.get<Document[]>(this.baseUrl + 'articles/').pipe(
      tap( () => console.log('get document list successful...'), () => console.error('get document list failed...'))
    );
  }

  getDocument(id: string): Observable<Document> {
    return this.http.get<Document>(this.baseUrl + 'articles/' + id).pipe(
      tap(() => console.log('get document successful...'), () => console.error('get document failed...'))
    );
  }
}
